I tried filtering messages based on the payload using Amazon SNS

I tried filtering messages based on the payload using Amazon SNS

Clock Icon2024.07.18

Introduction

Hello, this is Hemanth from the Alliance Department. In this blog, I will demonstrate how to filter messages based on payload using Amazon Simple Notification Service (Amazon SNS), which now supports payload-based message filtering. This expands its feature set, which already included attribute-based message filtering.

AWS

Is a secure cloud service platform that offers compute power, database storage, content delivery, network, and other functionality to help businesses scale and grow. It is one of the first cloud vendors to start services in the year 2006. It offers all the 3 service models namely IAAS, PAAS, and SAAS. Some of the notable domains in AWS are Compute, Migration, Storage, Network and Content Delivery, Management Tools, Database, Messaging, Security and Identity Compliance, and many more.

S3

Simple and popular AWS Service for storage. Replicates data by default across multiple facilities. It charges per usage. It is deeply integrated with AWS Services. Buckets are logical storage units. Objects are data added to the bucket. S3 has a storage class on object level which can save money by moving less frequently accessed objects to a colder storage class.

SNS

A fully managed messaging service for both application to application and application to person. A2A pub/sub functionality provides topics for high-throughput, push-based, many-to-many messaging between distributed systems, microservices, and event-driven serverless applications. The A2P functionality enables you to send messages to users at scale via SMS, mobile push, and email.

Lambda

A serverless compute service that runs code as a reply to events and automatically takes care of the bottom resources. It runs code on high availability compute infrastructure and performs all the administration of the compute resources. A few examples are HTTP requests vis Amazon API Gateway, changes to objects in S3, and many others.

Demo

Open the AWS Management Console and search for S3. Click on Create Bucket. Provide a unique bucket name. Leave everything else as default and click on Create Bucket.
Screenshot 2024-07-18 at 20.47.08
Screenshot 2024-07-18 at 20.49.11
Bucket successfully created.
Screenshot 2024-07-18 at 20.50.01
Go to the AWS Management Console and search for SNS. Click on Create Topic. Provide a topic name and click on Next Step.
Screenshot 2024-07-18 at 20.51.48
Change the access policy of subscribers and publishers to Everyone, keeping everything else as default.
Screenshot 2024-07-18 at 20.53.13
Click on Create Topic.
Screenshot 2024-07-18 at 20.54.43
SNS topic succesfully created
Screenshot 2024-07-18 at 20.56.10
Go to the AWS Management Console and search for Lambda. Click on Create Function. Provide a function name, choose runtime as Python, and click on Create Function.
Screenshot 2024-07-18 at 20.58.31
Function successfully created. Make changes to the code and deploy the function.
Screenshot 2024-07-18 at 20.59.55
In the Lambda function, click on Add Trigger.
Screenshot 2024-07-18 at 21.02.14
Select SNS and choose the previously created SNS topic.
Screenshot 2024-07-18 at 21.03.22
Go to the S3 bucket created earlier, click on Properties.
Screenshot 2024-07-18 at 21.06.03
Scroll down to Event notifications and click on Create event notification. Provide an event name and tick the All objects create events checkbox.
Screenshot 2024-07-18 at 21.07.51
Click on Save changes.
Screenshot 2024-07-18 at 21.08.55
Create a folder in the S3 bucket, name it "filtered", and click on Create Folder.
Screenshot 2024-07-18 at 21.17.59
Screenshot 2024-07-18 at 21.23.28
Go to the SNS topic created earlier, click on the subscription created by Lambda.
Screenshot 2024-07-18 at 21.36.58
Click on edit
Screenshot 2024-07-18 at 21.24.20
Turn on the Subscription filter policy and change the code of the message body to filter messages based on specific criteria (e.g., anything uploaded with "image" in the name). Click on Save changes.
Screenshot 2024-07-18 at 21.30.25
Successfully saved.
Screenshot 2024-07-18 at 21.31.32
In the Lambda function, click on View CloudWatch Logs.
Screenshot 2024-07-18 at 21.35.40
Check the log stream created by SNS when it was added to Lambda.
Screenshot 2024-07-18 at 21.36.58
Now going back to S3 and in the filtered folder uploading image
Screenshot 2024-07-18 at 21.39.15
Check CloudWatch logs to verify that Lambda was successfully triggered.
Screenshot 2024-07-18 at 21.44.19
Checking inside the log
Screenshot 2024-07-18 at 21.48.01
Check the metrics section in CloudWatch, under SNS, to see the invocation metrics.
Screenshot 2024-07-18 at 21.50.41
Upload another object to the "filtered" folder to test the filtering with another name as below.
Screenshot 2024-07-18 at 21.52.32
Check CloudWatch logs to verify that Lambda was triggered. This shows that lambda has not been triggered.
Screenshot 2024-07-18 at 21.54.24
Check the metrics section in CloudWatch, under SNS, to see the invocation metrics and you can notice new entries.
Screenshot 2024-07-18 at 21.56.02
Screenshot 2024-07-18 at 21.57.14

Conclusion

Amazon Simple Notification Service (SNS) has improved its attribute-based filtering capabilities by adding payload-based message filtering functionality. By using subscription filter policies that assess message content, this enables more accurate and effective message management. This feature helps you minimize pointless processing, simplify event-driven systems, and make sure apps receive only pertinent data. This enhancement creates new opportunities for scalable communications operations and streamlines system design.

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.